{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quickstart"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this quickstart, you'll learn how to use the dataset viewer's REST API to:\n",
    "\n",
    "- Check whether a dataset on the Hub is functional.\n",
    "- Return the subsets and splits of a dataset.\n",
    "- Preview the first 100 rows of a dataset.\n",
    "- Download slices of rows of a dataset.\n",
    "- Search a word in a dataset.\n",
    "- Filter rows based on a query string.\n",
    "- Access the dataset as parquet files.\n",
    "- Get the dataset size (in number of rows or bytes).\n",
    "- Get statistics about the dataset."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## API endpoints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each feature is served through an endpoint summarized in the table below:\n",
    "\n",
    "| Endpoint                    | Method | Description                                             | Query parameters                                                                                                                                                                                                                                  |\n",
    "|-----------------------------|--------|---------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n",
    "| [/is-valid](./valid)        | GET    | Check whether a specific dataset is valid.              | `dataset`: name of the dataset                                                                                                                                                                                                                    |\n",
    "| [/splits](./splits)         | GET    | Get the list of subsets and splits of a dataset. | `dataset`: name of the dataset                                                                                                                                                                                                                    |\n",
    "| [/first-rows](./first_rows) | GET    | Get the first rows of a dataset split.                  | - `dataset`: name of the dataset<br>- `config`: name of the config<br>- `split`: name of the split                                                                                                                                                |\n",
    "| [/rows](./rows)             | GET    | Get a slice of rows of a dataset split.                 | - `dataset`: name of the dataset<br>- `config`: name of the config<br>- `split`: name of the split<br>- `offset`: offset of the slice<br>- `length`: length of the slice (maximum 100)                                                            |\n",
    "| [/search](./search)         | GET    | Search text in a dataset split.                         | - `dataset`: name of the dataset<br>- `config`: name of the config<br>- `split`: name of the split<br>- `query`: text to search for<br>                                                                                                           |\n",
    "| [/filter](./filter)         | GET    | Filter rows in a dataset split.                         | - `dataset`: name of the dataset<br>- `config`: name of the config<br>- `split`: name of the split<br>- `where`: filter query<br>- `orderby`: order-by clause<br>- `offset`: offset of the slice<br>- `length`: length of the slice (maximum 100) |\n",
    "| [/parquet](./parquet)       | GET    | Get the list of parquet files of a dataset.             | `dataset`: name of the dataset                                                                                                                                                                                                                    |\n",
    "| [/size](./size)             | GET    | Get the size of a dataset.                              | `dataset`: name of the dataset                                                                                                                                                                                                                    |\n",
    "| [/statistics](./statistics) | GET    | Get statistics about a dataset split.                   | - `dataset`: name of the dataset<br>- `config`: name of the config<br>- `split`: name of the split                                                                                                                                                |\n",
    "| [/croissant](./croissant)   | GET    | Get Croissant metadata about a dataset.                 | - `dataset`: name of the dataset                                                                                                                                                                                                                  |\n",
    "\n",
    "There is no installation or setup required to use the dataset viewer API.\n",
    "\n",
    "<Tip>\n",
    "  Sign up for a <a href=\"https://huggingface.co/join\">Hugging Face account</a>{\" \"}\n",
    "  if you don't already have one! While you can use the dataset viewer API without a\n",
    "  Hugging Face account, you won't be able to access{\" \"}\n",
    "  <a href=\"https://huggingface.co/docs/hub/datasets-gated\">gated datasets</a>{\" \"}\n",
    "  like{\" \"}\n",
    "  <a href=\"https://huggingface.co/datasets/mozilla-foundation/common_voice_10_0\">\n",
    "    CommonVoice\n",
    "  </a>{\" \"}\n",
    "  and <a href=\"https://huggingface.co/datasets/ILSVRC/imagenet-1k\">ImageNet</a> without\n",
    "  providing a <a href=\"https://huggingface.co/settings/tokens\">user token</a>{\" \"}\n",
    "  which you can find in your user settings.\n",
    "</Tip>\n",
    "\n",
    "Feel free to try out the API in [Postman](https://www.postman.com/huggingface/workspace/hugging-face-apis/documentation/23242779-d068584e-96d1-4d92-a703-7cb12cbd8053), [ReDoc](https://redocly.github.io/redoc/?url=https://datasets-server.huggingface.co/openapi.json) or [RapidAPI](https://rapidapi.com/hugging-face-hugging-face-default/api/hugging-face-datasets-api/). This quickstart will show you how to query the endpoints programmatically.\n",
    "\n",
    "The base URL of the REST API is:\n",
    "\n",
    "```\n",
    "https://datasets-server.huggingface.co\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Private and gated datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For [private](https://huggingface.co/docs/hub/repositories-settings#private-repositories) and [gated](https://huggingface.co/docs/hub/datasets-gated) datasets, you'll need to provide your user token in `headers` of your query. Otherwise, you'll get an error message to retry with authentication.\n",
    "\n",
    "<Tip>\n",
    "\n",
    "The dataset viewer supports private datasets owned by a [PRO user](https://huggingface.co/pricing) or an [Enterprise Hub organization](https://huggingface.co/enterprise).\n",
    "\n",
    "</Tip>\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n",
    "API_URL = \"https://datasets-server.huggingface.co/is-valid?dataset=allenai/WildChat-nontoxic\"\n",
    "def query():\n",
    "    response = requests.get(API_URL, headers=headers)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/is-valid?dataset=allenai/WildChat-nontoxic\",\n",
    "        {\n",
    "            headers: { Authorization: `Bearer ${API_TOKEN}` },\n",
    "            method: \"GET\",\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/is-valid?dataset=allenai/WildChat-nontoxic \\\n",
    "        -X GET \\\n",
    "        -H \"Authorization: Bearer ${API_TOKEN}\"\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "You'll see the following error if you're trying to access a gated dataset without providing your user token:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(data)\n",
    "{'error': 'The dataset does not exist, or is not accessible without authentication (private or gated). Please check the spelling of the dataset name or retry with authentication.'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Check dataset validity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To check whether a specific dataset is valid, for example, [Rotten Tomatoes](https://huggingface.co/datasets/cornell-movie-review-data/rotten_tomatoes), use the `/is-valid` endpoint:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/is-valid?dataset=cornell-movie-review-data/rotten_tomatoes\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/is-valid?dataset=cornell-movie-review-data/rotten_tomatoes\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/is-valid?dataset=cornell-movie-review-data/rotten_tomatoes \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "This returns whether the dataset provides a preview (see /first-rows), the viewer (see /rows), the search (see /search) and the filter (see /filter), and statistics (see /statistics):\n",
    "\n",
    "```json\n",
    "{ \"preview\": true, \"viewer\": true, \"search\": true, \"filter\": true, \"statistics\": true }\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List configurations and splits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `/splits` endpoint returns a JSON list of the splits in a dataset:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/splits?dataset=cornell-movie-review-data/rotten_tomatoes\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/splits?dataset=cornell-movie-review-data/rotten_tomatoes\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/splits?dataset=cornell-movie-review-data/rotten_tomatoes \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "This returns the available subsets and splits in the dataset:\n",
    "\n",
    "```json\n",
    "{\n",
    "  \"splits\": [\n",
    "    { \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\", \"config\": \"default\", \"split\": \"train\" },\n",
    "    {\n",
    "      \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "      \"config\": \"default\",\n",
    "      \"split\": \"validation\"\n",
    "    },\n",
    "    { \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\", \"config\": \"default\", \"split\": \"test\" }\n",
    "  ],\n",
    "  \"pending\": [],\n",
    "  \"failed\": []\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preview a dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `/first-rows` endpoint returns a JSON list of the first 100 rows of a dataset. It also returns the types of data features (\"columns\" data types). You should specify the dataset name, subset name (you can find out the subset name from the `/splits` endpoint), and split name of the dataset you'd like to preview:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/first-rows?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/first-rows?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/first-rows?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "This returns the first 100 rows of the dataset:\n",
    "\n",
    "```json\n",
    "{\n",
    "  \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "  \"config\": \"default\",\n",
    "  \"split\": \"train\",\n",
    "  \"features\": [\n",
    "    {\n",
    "      \"feature_idx\": 0,\n",
    "      \"name\": \"text\",\n",
    "      \"type\": { \"dtype\": \"string\", \"_type\": \"Value\" }\n",
    "    },\n",
    "    {\n",
    "      \"feature_idx\": 1,\n",
    "      \"name\": \"label\",\n",
    "      \"type\": { \"names\": [\"neg\", \"pos\"], \"_type\": \"ClassLabel\" }\n",
    "    }\n",
    "  ],\n",
    "  \"rows\": [\n",
    "    {\n",
    "      \"row_idx\": 0,\n",
    "      \"row\": {\n",
    "        \"text\": \"the rock is destined to be the 21st century's new \\\" conan \\\" and that he's going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .\",\n",
    "        \"label\": 1\n",
    "      },\n",
    "      \"truncated_cells\": []\n",
    "    },\n",
    "    {\n",
    "      \"row_idx\": 1,\n",
    "      \"row\": {\n",
    "        \"text\": \"the gorgeously elaborate continuation of \\\" the lord of the rings \\\" trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson's expanded vision of j . r . r . tolkien's middle-earth .\",\n",
    "        \"label\": 1\n",
    "      },\n",
    "      \"truncated_cells\": []\n",
    "    },\n",
    "    ...,\n",
    "    ...\n",
    "  ]\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download slices of a dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `/rows` endpoint returns a JSON list of a slice of rows of a dataset at any given location (offset).\n",
    "It also returns the types of data features (\"columns\" data types).\n",
    "You should specify the dataset name, subset name (you can find out the subset name from the `/splits` endpoint), the split name and the offset and length of the slice you'd like to download:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/rows?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train&offset=150&length=10\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/rows?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train&offset=150&length=10\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/rows?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train&offset=150&length=10 \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "You can download slices of 100 rows maximum at a time.\n",
    "\n",
    "The response looks like:\n",
    "\n",
    "```json\n",
    "{\n",
    "  \"features\": [\n",
    "    {\n",
    "      \"feature_idx\": 0,\n",
    "      \"name\": \"text\",\n",
    "      \"type\": { \"dtype\": \"string\", \"_type\": \"Value\" }\n",
    "    },\n",
    "    {\n",
    "      \"feature_idx\": 1,\n",
    "      \"name\": \"label\",\n",
    "      \"type\": { \"names\": [\"neg\", \"pos\"], \"_type\": \"ClassLabel\" }\n",
    "    }\n",
    "  ],\n",
    "  \"rows\": [\n",
    "    {\n",
    "      \"row_idx\": 150,\n",
    "      \"row\": {\n",
    "        \"text\": \"enormously likable , partly because it is aware of its own grasp of the absurd .\",\n",
    "        \"label\": 1\n",
    "      },\n",
    "      \"truncated_cells\": []\n",
    "    },\n",
    "    {\n",
    "      \"row_idx\": 151,\n",
    "      \"row\": {\n",
    "        \"text\": \"here's a british flick gleefully unconcerned with plausibility , yet just as determined to entertain you .\",\n",
    "        \"label\": 1\n",
    "      },\n",
    "      \"truncated_cells\": []\n",
    "    },\n",
    "    ...,\n",
    "    ...\n",
    "  ],\n",
    "  \"num_rows_total\": 8530,\n",
    "  \"num_rows_per_page\": 100,\n",
    "  \"partial\": false\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Search text in a dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `/search` endpoint returns a JSON list of a slice of rows of a dataset that match a text query. The text is searched in the columns of type `string`, even if the values are nested in a dictionary.\n",
    "It also returns the types of data features (\"columns\" data types). The response format is the same as the /rows endpoint.\n",
    "You should specify the dataset name, subset name (you can find out the subset name from the `/splits` endpoint), the split name and the search query you'd like to find in the text columns:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/search?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train&query=cat\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/search?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train&query=cat\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/search?dataset=cornell-movie-review-data/rotten_tomatoes&config=default&split=train&query=cat \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "You can get slices of 100 rows maximum at a time, and you can ask for other slices using the `offset` and `length` parameters, as for the `/rows` endpoint.\n",
    "\n",
    "The response looks like:\n",
    "\n",
    "```json\n",
    "{\n",
    "  \"features\": [\n",
    "    {\n",
    "      \"feature_idx\": 0,\n",
    "      \"name\": \"text\",\n",
    "      \"type\": { \"dtype\": \"string\", \"_type\": \"Value\" }\n",
    "    },\n",
    "    {\n",
    "      \"feature_idx\": 1,\n",
    "      \"name\": \"label\",\n",
    "      \"type\": { \"dtype\": \"int64\", \"_type\": \"Value\" }\n",
    "    }\n",
    "  ],\n",
    "  \"rows\": [\n",
    "    {\n",
    "      \"row_idx\": 9,\n",
    "      \"row\": {\n",
    "        \"text\": \"take care of my cat offers a refreshingly different slice of asian cinema .\",\n",
    "        \"label\": 1\n",
    "      },\n",
    "      \"truncated_cells\": []\n",
    "    },\n",
    "    {\n",
    "      \"row_idx\": 472,\n",
    "      \"row\": {\n",
    "        \"text\": \"[ \\\" take care of my cat \\\" ] is an honestly nice little film that takes us on an examination of young adult life in urban south korea through the hearts and minds of the five principals .\",\n",
    "        \"label\": 1\n",
    "      },\n",
    "      \"truncated_cells\": []\n",
    "    },\n",
    "    ...,\n",
    "    ...\n",
    "  ],\n",
    "  \"num_rows_total\": 12,\n",
    "  \"num_rows_per_page\": 100,\n",
    "  \"partial\": false\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Access Parquet files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The dataset viewer converts every dataset on the Hub to the [Parquet](https://parquet.apache.org/) format. The `/parquet` endpoint returns a JSON list of the Parquet URLs for a dataset:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/parquet?dataset=cornell-movie-review-data/rotten_tomatoes\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/parquet?dataset=cornell-movie-review-data/rotten_tomatoes\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});\n",
    "```\n",
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/parquet?dataset=cornell-movie-review-data/rotten_tomatoes \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "This returns a URL to the Parquet file for each split:\n",
    "\n",
    "```json\n",
    "{\n",
    "  \"parquet_files\": [\n",
    "    {\n",
    "      \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "      \"config\": \"default\",\n",
    "      \"split\": \"test\",\n",
    "      \"url\": \"https://huggingface.co/datasets/cornell-movie-review-data/rotten_tomatoes/resolve/refs%2Fconvert%2Fparquet/default/test/0000.parquet\",\n",
    "      \"filename\": \"0000.parquet\",\n",
    "      \"size\": 92206\n",
    "    },\n",
    "    {\n",
    "      \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "      \"config\": \"default\",\n",
    "      \"split\": \"train\",\n",
    "      \"url\": \"https://huggingface.co/datasets/cornell-movie-review-data/rotten_tomatoes/resolve/refs%2Fconvert%2Fparquet/default/train/0000.parquet\",\n",
    "      \"filename\": \"0000.parquet\",\n",
    "      \"size\": 698845\n",
    "    },\n",
    "    {\n",
    "      \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "      \"config\": \"default\",\n",
    "      \"split\": \"validation\",\n",
    "      \"url\": \"https://huggingface.co/datasets/cornell-movie-review-data/rotten_tomatoes/resolve/refs%2Fconvert%2Fparquet/default/validation/0000.parquet\",\n",
    "      \"filename\": \"0000.parquet\",\n",
    "      \"size\": 90001\n",
    "    }\n",
    "  ],\n",
    "  \"pending\": [],\n",
    "  \"failed\": [],\n",
    "  \"partial\": false\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get the size of the dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `/size` endpoint returns a JSON with the size (number of rows and size in bytes) of the dataset, and for every subset and split:\n",
    "\n",
    "<inferencesnippet>\n",
    "<python>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_URL = \"https://datasets-server.huggingface.co/size?dataset=cornell-movie-review-data/rotten_tomatoes\"\n",
    "def query():\n",
    "    response = requests.get(API_URL)\n",
    "    return response.json()\n",
    "data = query()\n",
    "````\n",
    "\n",
    "</python>\n",
    "<js>\n",
    "```js\n",
    "import fetch from \"node-fetch\";\n",
    "async function query(data) {\n",
    "    const response = await fetch(\n",
    "        \"https://datasets-server.huggingface.co/size?dataset=cornell-movie-review-data/rotten_tomatoes\",\n",
    "        {\n",
    "            method: \"GET\"\n",
    "        }\n",
    "    );\n",
    "    const result = await response.json();\n",
    "    return result;\n",
    "}\n",
    "query().then((response) => {\n",
    "    console.log(JSON.stringify(response));\n",
    "});"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</js>\n",
    "<curl>\n",
    "```curl\n",
    "curl https://datasets-server.huggingface.co/size?dataset=cornell-movie-review-data/rotten_tomatoes \\\n",
    "        -X GET\n",
    "```\n",
    "</curl>\n",
    "</inferencesnippet>\n",
    "\n",
    "This returns the size of the dataset, and for every subset and split:\n",
    "\n",
    "```json\n",
    "{\n",
    "  \"size\": {\n",
    "    \"dataset\": {\n",
    "      \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "      \"num_bytes_original_files\": 487770,\n",
    "      \"num_bytes_parquet_files\": 881052,\n",
    "      \"num_bytes_memory\": 1345449,\n",
    "      \"num_rows\": 10662\n",
    "    },\n",
    "    \"configs\": [\n",
    "      {\n",
    "        \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "        \"config\": \"default\",\n",
    "        \"num_bytes_original_files\": 487770,\n",
    "        \"num_bytes_parquet_files\": 881052,\n",
    "        \"num_bytes_memory\": 1345449,\n",
    "        \"num_rows\": 10662,\n",
    "        \"num_columns\": 2\n",
    "      }\n",
    "    ],\n",
    "    \"splits\": [\n",
    "      {\n",
    "        \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "        \"config\": \"default\",\n",
    "        \"split\": \"train\",\n",
    "        \"num_bytes_parquet_files\": 698845,\n",
    "        \"num_bytes_memory\": 1074806,\n",
    "        \"num_rows\": 8530,\n",
    "        \"num_columns\": 2\n",
    "      },\n",
    "      {\n",
    "        \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "        \"config\": \"default\",\n",
    "        \"split\": \"validation\",\n",
    "        \"num_bytes_parquet_files\": 90001,\n",
    "        \"num_bytes_memory\": 134675,\n",
    "        \"num_rows\": 1066,\n",
    "        \"num_columns\": 2\n",
    "      },\n",
    "      {\n",
    "        \"dataset\": \"cornell-movie-review-data/rotten_tomatoes\",\n",
    "        \"config\": \"default\",\n",
    "        \"split\": \"test\",\n",
    "        \"num_bytes_parquet_files\": 92206,\n",
    "        \"num_bytes_memory\": 135968,\n",
    "        \"num_rows\": 1066,\n",
    "        \"num_columns\": 2\n",
    "      }\n",
    "    ]\n",
    "  },\n",
    "  \"pending\": [],\n",
    "  \"failed\": [],\n",
    "  \"partial\": false\n",
    "}\n",
    "```"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}
